<?php 
/*
   Copyright 2010, Sanket Parab

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
   This is a Test User Interface meant for demo purposes.
 */
function mergeAttrs($attrs=array()) {
	$mergedAttrs = $attrs;
	if(isset($attrs)) {
		foreach ($attrs as $key => $value) {
			if (strtolower($key) == 'class') {
				if($mergedAttrs[$key] != $value)
				$value = $mergedAttrs[$key] . ' ' . $value;
			} else if (strtolower($key) == 'style') {
				if($mergedAttrs[$key] != $value)
				$value = $mergedAttrs[$key] . ';' . $value;
			}
		}
		$mergedAttrs[$key] = $value;
	}
	return $mergedAttrs;
}

function getAttrString($attrs=array()) {
	$attrPairs = array();
	if(isset($attrs)) {
		foreach ($attrs as $name => $value) {
			$value = htmlentities($value, ENT_QUOTES);
			$attrPairs[] = "$name=\"$value\"";
		}
	}
	return join(' ', $attrPairs);
}

function anchor($href,$label,$attrs=array()) {
	$selected = false;
	$id = generateId($href);
	$len = strlen($href);
	$url_folder = substr(URL,0,strpos(URL,'/'));
	$href_folder = substr($href,0,strpos($href,'/'));
	if(strpos($href,'/') == $len-1 && strpos($href_folder,'.') == false && $url_folder == $href_folder) {
		$selected = true;
	} else if ($href == URL) {
		$selected = true;
	}
	if($href[0] == '#') {
		$href = URL.$href;
	}
	if(isset($attrs['noselect'])) {
		if($attrs['noselect'] == false) {
			$selected = false;
		}
	}
	$url = 'http://'.HOST."/$href";
	if($selected == true) {
		if(isset($attrs['class'])) {
			$attrs['class'] = $attrs['class'].' selected';
		} else {
			$attrs['class'] = 'selected';
		}
	}
	$attr_string = getAttrString($attrs);
	$body = "<a $attr_string href='$url'>$label</a>";
	return $body;
}

function newText($name,$label,$attrs=array(),$stack=true) {
	$id = generateId($name);
	$type = $attrs['type']? $attrs['type'] : 'text';
	if(isset($attrs['description'])) { $description = $attrs['description']; unset($attrs['description']); }
	if(isset($attrs['class'])) { $div['class'] = $attrs['class']; unset($attrs['class']); }
	if(isset($attrs['style'])) { $div['style'] = $attrs['style']; unset($attrs['style']); }
	$divString = getAttrString(mergeAttrs($div));
	$attrString = getAttrString($attrs);
	$labelStr = $label === ''? '' : "<label id='lbl-$id' for='$id'>$label: </label>";
	$body =	"$labelStr
			<input id='$id' name='$name' type='$type' $attrString>
			<span id='$id-validate' class='invisible hover'></span>
			$description";
			if($stack===true) {
				return "<div id='row-$id' $divString>$body</div>";
			} else {
				return $body;
			}
}

function newTextArea($name,$label,$attrs=array(),$stack=true) {
	$id = generateId($name);
	if(isset($attrs['description'])) { $description = $attrs['description']; unset($attrs['description']); }
	if(isset($attrs['class'])) { $div['class'] = $attrs['class']; unset($attrs['class']); }
	if(isset($attrs['style'])) { $div['style'] = $attrs['style']; unset($attrs['style']); }
	$divString = getAttrString(mergeAttrs($div));
	$attrString = getAttrString($attrs);
	$labelStr = $label === ''? '' : "<label id='lbl-$id' for='$id' style='vertical-align:top;'>$label: </label>";
	$body =	"$labelStr
			<textarea id='$id' name='$name' $attrString></textarea>
			<span id='$id-validate' class='invisible hover'></span>
			$description";
			if($stack===true) return "<div id='row-$id' $divString>$body</div>";
			else return $body;
}

function newFileUpload($name,$attrs=array()) {
	$id = generateId($name);
	$body = "<input name='$name' type='file' id='$id'>";
	return $body;
}

function newBtn($name,$label,$attrs=array(),$stack=true) {
	$id = generateId($name);
	if($stack == true) {
		if(isset($attrs['class'])) { $div['class'] = $attrs['class']; unset($attrs['class']); }
		if(isset($attrs['style'])) { $div['style'] = $attrs['style']; unset($attrs['style']); }
		if(isset($div)) {
			$div_string = getAttrString(mergeAttrs($div));
		} else {
			$div_string = '';
		}
	}
	if(!isset($attrs['type'])) {
		$attrs['type'] = 'submit';
	}
	$attr_string = getAttrString($attrs);
	$body = "<input id='$id' name='$name' $attr_string value='$label'>";
	if($stack == true) {
		return "<div id='row-$id' $div_string>$body</div>";
	} else {
		return $body;
	}
}

function button($name,$label,$attrs=array(),$stack=true) {
	$id = generateId($name);
	if($stack == true) {
		if(isset($attrs['class'])) { $div['class'] = $attrs['class']; unset($attrs['class']); }
		if(isset($attrs['style'])) { $div['style'] = $attrs['style']; unset($attrs['style']); }
		if(isset($div)) {
			$div_string = getAttrString(mergeAttrs($div));
		} else {
			$div_string = '';
		}
	}
	if(!isset($attrs['type'])) {
		$attrs['type'] = 'submit';
	}
	$attr_string = getAttrString($attrs);
	$body = "<button id='$id' name='$name' $attr_string>$label</button>";
	if($stack === true) {
		return "<div id='row-$id' $div_string>$body</div>";
	} else {
		return $body;
	}
}

function imageAnchor($href,$id,$image,$width,$height,$class_name='') {
	$onmouseover = $class_name? "this.className=&#39img-btn-hover $class_name&#39" : "this.className=&#39img-btn-hover&#39";
	$onmouseout = $class_name? "this.className=&#39img-btn $class_name&#39" : "this.className=&#39img-btn&#39";
	$onmousedown = $class_name? "this.className=&#39img-btn-down $class_name&#39" : "this.className=&#39img-btn-down&#39";
	$class = $class_name? "class='$class_name'" : "";
	return "
	<div id='$id' $class>
	<a href='$href' class='img-btn'
	style='background:url(/includes/images/btn/$image) no-repeat scroll left top;width:{$width}px;height:{$height}px'
	onmouseover='$onmouseover' onmouseout='$onmouseout' onmousedown='$onmousedown'>&nbsp;</a>
	</div>";
}

function newTabs($data,$width) {
	$count = 0;
	$body = "<div id='tabs-bar'>";
	foreach($data as $key=>$value) {
		if($value) {
			$count++;
			$id = generateId($key);
			$pClass = $count === 1? 'selected' : '';
			$divClass = $count === 1? 'tab selected' : 'tab';
			$body .= "<div id='tab-$id' class='$divClass'>
				  <p id='$id'
				  onclick='javascript:showTab(&#39;$id&#39;,&#39;$count&#39;)'
				  class='$pClass'>$key</p></div>";
		}
	}
	$body .= "</div><div id='tabs-content'>";
	$count = 0;
	foreach($data as $key=>$value) {
		if($value) {
			$count++;
			$id = generateId($key);
			$class = $count === 1? 'visible' : 'invisible';
			$body .= "<div id='content-$id' style='width:$width;' class='$class'>$value</div>";
		}
	}
	$body .= "</div><div class='clear'></div>";
	return $body;
}
?>